package cn.edu.suse.epilepsy.service.impl;

import cn.edu.suse.epilepsy.common.BizException;
import cn.edu.suse.epilepsy.entity.dto.DepAddInfoDto;
import cn.edu.suse.epilepsy.entity.dto.DepUpdateInfoDto;
import cn.edu.suse.epilepsy.entity.pojo.DepInfoPojo;
import cn.edu.suse.epilepsy.mapper.DepInfoMapper;
import cn.edu.suse.epilepsy.service.DepInfoService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;

import java.util.Date;
import java.util.List;

@Service
@Transactional(rollbackFor = Throwable.class)
public class DepInfoServiceImpl extends ServiceImpl<DepInfoMapper, DepInfoPojo> implements DepInfoService {
    @Autowired
    private DepInfoMapper depInfoMapper;
    @Override
    public DepInfoPojo addInfo(DepInfoPojo depInfoPojo) {
        return null;
    }

    @Override
    public List<DepInfoPojo> findInfoById(Integer id) {
        QueryWrapper<DepInfoPojo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("dep_info_id",id);
        return depInfoMapper.selectList(queryWrapper);
    }

    @Override
    public List<DepInfoPojo> findAllInfo() {
        QueryWrapper<DepInfoPojo> queryWrapper = new QueryWrapper<>();

        return depInfoMapper.selectList(queryWrapper);
    }

    @Override
    public DepInfoPojo addInfo(DepAddInfoDto depAddInfoDto) throws BizException {
        if (StringUtils.isEmpty(depAddInfoDto.getDepName())||StringUtils.isEmpty(depAddInfoDto.getDepDirector())||StringUtils.isEmpty(depAddInfoDto.getDepSuperior())||StringUtils.isEmpty(depAddInfoDto.getPhone())){
            throw new BizException("所有字段都必填的");
        }
        LambdaQueryWrapper<DepInfoPojo> wrapper = Wrappers.lambdaQuery(DepInfoPojo.class)
                .eq(DepInfoPojo::getDepDirector,depAddInfoDto.getDepDirector())
                .eq(DepInfoPojo::getPhone,depAddInfoDto.getPhone());
        DepInfoPojo existingInfo = depInfoMapper.selectOne(wrapper);
        if (existingInfo != null) {
            throw new BizException("该部门已存在");
        }
        DepInfoPojo newDepInfo = new DepInfoPojo();
        BeanUtils.copyProperties(depAddInfoDto,newDepInfo);
        newDepInfo.setCreated(new Date());
        newDepInfo.setDepName(depAddInfoDto.getDepName());
        newDepInfo.setDepSuperior(depAddInfoDto.getDepSuperior());
        newDepInfo.setDepDirector(depAddInfoDto.getDepDirector());
        newDepInfo.setPhone(depAddInfoDto.getPhone());
        newDepInfo.setPersonCount(depAddInfoDto.getPersonCount());
        int result = depInfoMapper.insert(newDepInfo);
        if (result != 1){
            throw new BizException("添加部门失败，请重试");
        }
        return newDepInfo;
    }

    @Override
    public int deleteInfo(@PathVariable Integer id) {

        return depInfoMapper.deleteById(id);
    }

    @Override
    public void updateInfo(DepUpdateInfoDto depUpdateInfoDto) {
        Integer id=depUpdateInfoDto.getId();
        DepInfoPojo depInfoPojo = depInfoMapper.selectById(depUpdateInfoDto.getId());
        depInfoPojo.setDepDirector(depUpdateInfoDto.getDepDirector());
        depInfoPojo.setDepSuperior(depUpdateInfoDto.getDepSuperior());
        depInfoPojo.setPhone(depUpdateInfoDto.getPhone());
        depInfoPojo.setDepName(depUpdateInfoDto.getDepName());
        depInfoMapper.update(depInfoPojo,new QueryWrapper<DepInfoPojo>().eq("dep_info_id",id));
    }
}
